package com.example.xinli.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import com.example.xinli.HomeMoreActivity;
import com.example.xinli.InteractionActivity;
import com.example.xinli.R;
import com.example.xinli.custom.LineChartView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import java.util.Random;

public class HomeFragment extends Fragment {
    private ImageButton moreBtn;
    private TextView AITV;
    private LineChartView weektrendTV;

    @SuppressLint("MissingInflatedId")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);

        // 初始化按钮并设置点击事件
        moreBtn = view.findViewById(R.id.moreBtn);
        if (moreBtn != null) {
            moreBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    toMore(v);
                }
            });
        }

        AITV = view.findViewById(R.id.AITV);
        AITV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toAI(v);
            }
        });

        // 初始化本周趋势折线图
        weektrendTV = view.findViewById(R.id.weektrendTV);
        displayWeeklyTrend();

        return view;
    }

    public void toMore(View view) {
        Intent intent = new Intent(getActivity(), HomeMoreActivity.class);
        startActivity(intent);
    }

    public void toAI(View view) {
        Intent intent = new Intent(getActivity(), InteractionActivity.class);
        startActivity(intent);
    }

    /**
     * 显示本周运动趋势折线图
     */
    private void displayWeeklyTrend() {
        if (weektrendTV == null) return;

        // 生成最近7天的日期标签
        List<String> labels = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd", Locale.getDefault());

        // 从6天前到今天
        for (int i = 6; i >= 0; i--) {
            Calendar tempCalendar = (Calendar) calendar.clone();
            tempCalendar.add(Calendar.DAY_OF_YEAR, -i);
            labels.add(dateFormat.format(tempCalendar.getTime()));
        }

        // 生成随机运动量数据（实际项目中应该从数据库获取）
        List<Integer> dataPoints = new ArrayList<>();
        Random random = new Random();
        for (int i = 0; i < 7; i++) {
            int steps = 3000 + random.nextInt(7000); // 3000-10000步
            dataPoints.add(steps);
        }

        // 设置折线图数据
        weektrendTV.setData(dataPoints, labels);
        weektrendTV.setMaxValue(10000); // 设置Y轴最大值
    }
}
